<?php

namespace addons\Flow\common\models;

use Yii;
use common\models\merchant\Member;

/**
 * This is the model class for table "{{%addon_flow_workflow}}".
 *
 * @property int $id
 * @property int $status 状态
 * @property string $created_at 创建时间
 * @property string $updated_at 更新时间
 */
class Status extends \common\models\base\BaseModel
{

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%addon_flow_status}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id', 'workflow_id', 'label'], 'required'],
            [['id','workflow_id','label'], 'string', 'max' => 50],
            [['merchant_id','audit_id','role_id','type','sort'], 'integer'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => '状态ID',
            'label' => '状态名称',
            'workflow_id' => '工作流',
            'sort' => '排序',
            'type' => '审核类型',
            'audit_id' => '审核人员',
            'role_id' => '审核角色',
        ];
    }
    public function getMember()
    {
        return $this->hasOne(Member::className(), ['id' => 'audit_id'])->andWhere(['merchant_id'=>Yii::$app->user->identity->merchant_id]);
    }
    public function getMetadata()
    {
        return $this->hasOne(Metadata::className(), ['status_id' => 'id'])->andWhere(['workflow_id' => $this->workflow_id])->andWhere(['merchant_id'=>Yii::$app->user->identity->merchant_id]);
    }
    public function getMetadatas()
    {
        return $this->hasMany(Metadata::className(), ['status_id' => 'id'])->andWhere(['workflow_id' => $this->workflow_id])->andWhere(['merchant_id'=>Yii::$app->user->identity->merchant_id]);
    }
    public function getWorkflow()
    {
        return $this->hasOne(Workflow::className(), ['id' => 'workflow_id'])->andWhere(['merchant_id'=>Yii::$app->user->identity->merchant_id]);
    }
    public function getStartTransitions()
    {
        return $this->hasMany(Transition::className(), ['start_status_id' => 'id'])
            ->andWhere(['{{%addon_flow_transition}}.workflow_id' => $this->workflow_id])
            ->andWhere(['{{%addon_flow_transition}}.merchant_id' => Yii::$app->user->identity->merchant_id])
            ->leftJoin('{{%addon_flow_status}}', '{{%addon_flow_status}}.id = {{%addon_flow_transition}}.end_status_id AND {{%addon_flow_status}}.workflow_id = :workflow_id', [
                ':workflow_id' => $this->workflow_id,
            ])
            ->orderBy(['{{%addon_flow_status}}.sort' => SORT_ASC]);
    }
}
